(function($){
	$.fn.jqconfirm = function(options) {
	
		//Assign default values. These can be over-written by defined options from the calling function.
		var defaults = {
			msg: 'This link will take you away from this site.<br />',
			msg_proceed: 'Continue?',
			msg_cancel: 'Cancel!',
			screenbgcolor: '#003D1F',
			msgborder: '2px solid #008040',
			msgbgcolor: '#00BD5E',
			msgcolor: '#FFFFFF',
			msgheight: '150',
			msgwidth: '200',
			linkborder: '1px solid #FF6633'
		},
		
		//Here we merge the options & defaults into the "settings" variable.
		settings = $.extend({}, defaults, options);
		
		//Iterate through elements passed to jqconfirm and make sure that they are links.
		//If they are valid links, attach the onClick function.
		return this.each(function() {
			if($(this).is('a') && $(this).attr('href') != ''){
				var href = $(this).attr('href');
				$(this).click(screen);
				$(this).blur(function () {
					$(this).attr('href',href);
				});
			}
		});
		
		//Definition of the onClick function attached to selected links.
		//Basically we're just appending a "screen" and "dialog" box to the page and presenting the defined variables and confirmation links.
		function screen() {
			
			$(this).focus();
			//assign some important variables. Required for positioning and presenting the confirmed link.
			var href = $(this).attr('href'),
			divLeft = document.documentElement.clientWidth/2 - settings.msgwidth/2,
			divTop = document.documentElement.clientHeight/2 - settings.msgheight/2;
			//Remove the link's href attribute.
			$(this).removeAttr('href');
			//Append-Prepend the screen & dialog box elements with styles.
			$('body').append('<div id="jqconfirm_screen"></div>');
			$('#jqconfirm_screen').css({
				backgroundColor: settings.screenbgcolor,
				display: 'none'
				});
			$('body').append('<div id="jqconfirm_message"></div>');
			$('#jqconfirm_message').css({
				color: settings.msgcolor,
				border: settings.msgborder,
				backgroundColor: settings.msgbgcolor,
				height: settings.msgheight+'px',
				width: settings.msgwidth+'px',
				left: divLeft+'px',
				bottom: divTop+'px',
				display: 'none'
				});
			$('#jqconfirm_message').prepend(settings.msg+'<br /><div id="jqconfirm_link_container"></div>');
			$('#jqconfirm_link_container').prepend('<a id="jqconfirm_link" href="'+href+'">'+settings.msg_proceed+'</a><a id="jqconfirm_link" class="jqconfirm_hide" href="Javascript:void(0)">'+settings.msg_cancel+'</a>');
			//Finally, show the appended-prepended elements.
			$('#jqconfirm_screen').show();
			$('#jqconfirm_message').fadeIn('slow');
			//Attach an onClick to the "Cancel" link that hides the elements created above.
			$('.jqconfirm_hide').click(function () {
				$('#jqconfirm_message').remove();
				$('#jqconfirm_screen').remove();
			});
		}
	}
})(jQuery);
